package com.zl.quickdoc.format.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import com.zl.quickdoc.constant.QuickDocC;
import com.zl.quickdoc.format.FreemarkerTemplater;
import com.zl.quickdoc.format.IApiFormat;
import com.zl.quickdoc.model.ApiDocument;
import com.zl.quickdoc.model.DocCfgParam;

/**
 * @author zhouliang
 */
public class ApiFormatMarkdown extends ApiFormatBase implements IApiFormat {

    private FreemarkerTemplater templater = new FreemarkerTemplater("classpath:"+QuickDocC.TEMPLATE_ROOT+QuickDocC.TEMPLATE_MARKDOWN);
    
    @Override
    public String formatType() {
        return QuickDocC.DOC_TYPE_MARKDOWN;
    }
    @Override
    public String templateName() {
        return QuickDocC.TEMPLATE_MARKDOWN;
    }
    
    @Override
    public void format(ApiDocument apiDoc, DocCfgParam cfg) throws Exception{
        File tFile = getTemplatePath(cfg, templateName());
        if(tFile.exists()&&tFile.isFile()) {
            templater.setPath(tFile.getPath());
        }
        Map<String, Object> model = new HashMap<>();
        model.putAll(apiDoc.getProperties());
        model.put("apiModules", apiDoc.getApiModules());
        model.put("changeList", joinChangeContent(apiDoc.getChangedata(),";;  ","<br>","&nbsp;&nbsp;&nbsp;&nbsp;"));
        String s = templater.processString(model);
        File outfile = new File(cfg.getApiPath(),this.getFileTitle(cfg)+".md");
        FileOutputStream out = new FileOutputStream(outfile);
        writeDoc(s, out);
        apiDoc.setOutputPath(outfile.getAbsolutePath());
    }
}
